package com.ruoyi.carShop.service.impl;


import com.ruoyi.system.api.domain.Merchant;
import com.ruoyi.system.api.domain.MerchantImgs;
import com.ruoyi.system.api.domain.MerchantVerification;
import com.ruoyi.carShop.domain.RegisterAndChange;
import com.ruoyi.carShop.mapper.MerchantVerificationMapper;
import com.ruoyi.carShop.service.MerchantCarService;
import com.ruoyi.carShop.service.RegisterService;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private MerchantCarService carService;
    @Autowired
    private MerchantVerificationMapper merchantVerificationMapper;

    @Override
    public int register(RegisterAndChange registerAndChange) {
        String img = registerAndChange.getImg();
//        创建对象，并且给对象的属性赋值
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(registerAndChange, merchant);
        MerchantVerification verification = new MerchantVerification();
        BeanUtils.copyProperties(registerAndChange,verification);
//        判断商户是否存在，如果存在直接返回0，不存在进行新增操作
        if (merchant.getMerchantId()!=null){
            return 0;
        }
        //用户新增
        AjaxResult add = carService.add(merchant);
        //新增失败
        if (!add.isSuccess()){
            return 0;
        }
//        获取新增的商户id
        String name = merchant.getMerchantName();
        Merchant merchant2 = merchantVerificationMapper.selectByName(name);
        Long mid = null;
        if (merchant2.getMerchantId()==null){
            return 0;
        }else {
            mid = merchant2.getMerchantId();
        }
//        更新商家照片消息
        MerchantImgs imgs = new MerchantImgs();
        imgs.setImg(img);
        imgs.setMid(mid);
        //新增操作
        Integer i = merchantVerificationMapper.insertMerchantImgs(imgs);
        if(i==0){
            //商户门店照片信息新增失败
            return 0;
        }
//       更新用户认证信息
        verification.setMid(mid);
//        商户认证表新增
        Integer insertNum = merchantVerificationMapper.insert(verification);
        if (insertNum==0){
            return 0;
        }
        return 1;
    }
}
